package com.tsinghua.server.base.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsinghua.server.common.Result;
import com.tsinghua.server.common.constant.UserConstants;
import com.tsinghua.server.base.entity.UserEntity;
import com.tsinghua.server.base.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/risk/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据主键获取用户对象
     * @param userId
     * @return
     */
    @GetMapping(value = "find/{userId}")
    public Result getUser(@PathVariable Long userId){
        return Result.succ(userService.getById(userId));
    }

    /**
     * 更新用户对象
     * @param user
     * @return
     */
    @PutMapping(value = "update")
    public Result updateUser(@RequestBody UserEntity user){
        return Result.succ(userService.updateById(user));
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping(value = "del/{userId}")
    public Result delUser(@PathVariable Long userId){
        return Result.succ(userService.removeById(userId));
    }


    /**
     * 获取用户列表
     * @return
     */
    @GetMapping(value = "list")
    public Result listUsers(){
        return Result.succ(userService.list());
    }

    /**
     * 分页获取用户
     * @param pageSize
     * @param pageNo
     * @return
     */
    @GetMapping(value = "pages")
    public Result pageUsers(@RequestParam (value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                            @RequestParam (value = "pageNo", required = false, defaultValue = "1") Integer pageNo){

        Page<UserEntity> page = new Page(pageNo,pageSize);
        return Result.succ(userService.page(page));
    }

    @PostMapping(value = "save")
    public Result saveUser(@RequestBody UserEntity user) {
        String password = DigestUtil.md5Hex(user.getPassword());
        user.setPassword(password);
        return Result.succ(user);
    }

    @GetMapping(value = "batch-save")
    public Result batchSaveUser() {
        String password = DigestUtil.md5Hex("123456");
        for (int i=0;i<20;i++){
            UserEntity user = new UserEntity();
            user.setId(Integer.toUnsignedLong(i));
            user.setUsername("test"+i);
            user.setPassword(password);
            user.setEmail("test_"+i+"@qq.com");
            user.setAge("223");
            userService.save(user);
        }
        return Result.succ(null);
    }

    @PostMapping("/getList")
    public Result getList(@RequestBody UserEntity user,
                          @RequestParam (value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                          @RequestParam (value = "pageNo", required = false, defaultValue = "1") Integer pageNo){
        return userService.getList(user,pageSize,pageNo);
    }

    @PostMapping(value = "addUser")
    public Result addUser(@RequestBody UserEntity user) {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUsername()))) {
            return Result.fail("新增用户'" + user.getUsername() + "'失败，登录账号已存在");
        }
        String password = DigestUtil.md5Hex(user.getPassword());
        user.setPassword(password);
        return userService.addUser(user);
    }



}
